package com.syyo.admin.controller;

import com.syyo.admin.common.anno.AnonymousAccess;
import com.syyo.admin.common.anno.SysLog;
import com.syyo.admin.domain.ResultVo;
import com.syyo.admin.utils.R;
import com.syyo.admin.domain.req.CoCompanyReq;
import com.syyo.admin.service.CoCompanyService;
import com.syyo.saas.common.domain.CompanyUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @author: wang
 * @date: 2021-08-11 10:34:16
 */
@Api(tags = "系统：saas企业管理")
@RestController
@RequestMapping("/coCompany")
public class CoCompanyController {

    @Autowired
    private CoCompanyService coCompanyService;

     /**
      * saas企业新增
      * @param
      * @return
      */
    @SysLog("saas企业新增")
    @ApiOperation("saas企业新增")
    @PreAuthorize("@syyo.check('system:coCompany:add')")
    @PostMapping
    public ResultVo add(@RequestBody CoCompanyReq req) {
        return R.ok(coCompanyService.add(req));
    }

     /**
      * saas企业删除
      * @param
      * @return
      */
    @SysLog("saas企业删除")
    @ApiOperation("saas企业删除")
    @PreAuthorize("@syyo.check('system:coCompany:del')")
    @DeleteMapping
    public ResultVo del(@RequestBody CoCompanyReq req) {
        return R.ok(coCompanyService.del(req));
    }

    /**
      * saas企业编辑
      * @param req
      * @return
      */
    @SysLog("saas企业编辑")
    @ApiOperation("saas企业编辑")
    @PreAuthorize("@syyo.check('system:coCompany:edit')")
    @PutMapping
    public ResultVo edit(@RequestBody CoCompanyReq req) {
        return R.ok(coCompanyService.edit(req));
    }

    /**
      * saas企业详情
      * @param
      * @return
      */
    @ApiOperation("saas企业详情")
    @GetMapping("/{id}")
    public ResultVo findOne(@PathVariable("id") Integer id) {
        return R.ok(coCompanyService.findOne(id));
    }

    /**
      * saas企业列表
      * @param
      * @return
      */
    @ApiOperation("saas企业列表")
    @GetMapping("/list/{pageNum}/{pageSize}")
    public ResultVo findPage(@PathVariable("pageNum") Integer pageNum,
                            @PathVariable("pageSize") Integer pageSize,
                            CoCompanyReq req) {
        return R.ok(coCompanyService.findPage(pageNum, pageSize, req));
    }

    /**
     * saas企业导出
     * @param
     * @return
     */
    @SysLog("saas企业导出")
    @ApiOperation("saas企业导出")
    @PreAuthorize("@syyo.check('system:coCompany:export')")
    @PostMapping("/export")
    public void findPage(HttpServletResponse response) {
        coCompanyService.export(response);
    }


    @PostMapping("/login")
    public String companyLogin(@RequestBody CompanyUser user){

        String db = coCompanyService.companyLogin(user);

        return db;
    }

    @AnonymousAccess
    @PostMapping("/db")
    public String getDb(CompanyUser user){
        String db = coCompanyService.companyLogin(user);
        return db;
    }
}